// member/pages/coupon/coupon.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let login = require('../../../common/script/login');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 10,
    listData: [],
    plate_desc: [],
    receive_desc: [],
    plate_number: ""
  },
  //删除
  delete(e) {
    const that = this;
    let id = e.currentTarget.dataset.id;
    my.showModal({
      content: '是否删除该车牌号？',
      success(res) {
        if (res.confirm) {
          let data = {
            id: id,
          }
          my.showLoading({
            title: '加载中...',
          })
          myRequest.EasyHttp.post(api.reqUrls.delLicensePlate, data).then(res => {
            if (res.data.status == 1) {
              that.getData()
            }
          }).catch(err => {
            console.log(err, 888)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //获取停车券
  getPark() {
    const that = this;
    let data = {
      platenumber: that.data.plate_number
    }
    my.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.receiveParkingCoupon, data).then(res => {
      my.hideLoading();
      if (res.data.status == 1) {
        my.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
        })
      } else {
        my.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
        })
      }
    }).catch(err => {
      console.log(err, 888)
    })
  },
  getData() {
    const that = this;
    const cardnumber = my.getStorageSync({
      key: 'cardnumber'
    }).data;
    let data = {
      cardnumber: cardnumber
    }
    my.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getLicensePlate, data).then(res => {
      my.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.list.length == 0) {
          that.setData({
            isWithout: false,
            listData: [],
            plate_number: ''
          })
        } else {
          that.setData({
            isWithout: true,
            listData: res.data.result.list,
            plate_number: res.data.result.list[0].plate_number
          })
        }
      } else {
        that.setData({
          listData: [],
          isWithout: false
        })
      }

    })
  },
  getInit() {
    const that = this;
    myRequest.EasyHttp.post(api.reqUrls.getTips).then(res => {
      that.setData({
        plate_desc: res.data.result.plate_desc,
        receive_desc: res.data.result.receive_desc,
      })
    })
  },
  toAddPark() {
    my.navigateTo({
      url: `/member/pages/addPark/addPark`,
    })
  },
  getParkData(){
    my.navigateTo({
      url: `/member/pages/parkList/parkList`,
    })
  },
  //获取会员信息
  getCardinfoData() {
    const that = this;

    myRequest.EasyHttp.post(api.reqUrls.membercardShow).then(res => {
      if (res.data.status == 1) {


        my.setStorageSync({
          key: 'isVip',
          data: true
        })
        my.setStorageSync({
          key: 'refresh',
          data: false
        })
        my.setStorageSync({
          key: 'isfoundingmember',
          data: res.data.result.member_data.isfoundingmember
        })
        my.setStorageSync({
          key: 'cardnumber',
          data: res.data.result.member_data.cardnumber
        })
        my.setStorageSync({
          key: 'totalamount',
          data: res.data.result.member_data.totalamount
        })
        my.setStorageSync({
          key: 'userName',
          data: res.data.result.member_data.name
        })
        my.setStorageSync({
          key: 'userPhone',
          data: res.data.result.member_data.phone
        })
        that.getData()
      } else if (res.data.status == -2) {

        my.setStorageSync({
          key: 'isVip',
          data: false
        })
        my.setStorageSync({
          key: 'refresh',
          data: false
        })
        my.setStorageSync({
          key: 'isfoundingmember',
          data: false
        })
        my.setStorageSync({
          key: 'cardnumber',
          data: ''
        })
        my.setStorageSync({
          key: 'totalamount',
          data: 0
        })
        my.setStorageSync({
          key: 'userName',
          data: ''
        })
        my.setStorageSync({
          key: 'userPhone',
          data: ''
        })
        my.showModal({
          showCancel: false,
          content: '请绑定会员卡',
          success(res) {
            if (res.confirm) {
              my.switchTab({
                url: '/pages/index/index'
              })
            }
          }
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    login.checkLogin().then(res => {
      that.getCardinfoData()
      that.getInit()
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getCardinfoData()
        that.getInit()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})